package chapt07.bitmaps;
// Bitmaps.java
// OpenGL SuperBible
// Demonstrates loading and displaying bitmaps
// Program by Richard S. Wright Jr.

import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;

public class Bitmaps
{
	static GLU glu = new GLU();
	
	// Bitmap of camp fire
	static byte fire[] = {
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0xc0,
		(byte)0x00, (byte)0x00, (byte)0x01, (byte)0xf0,
		(byte)0x00, (byte)0x00, (byte)0x07, (byte)0xf0,
		(byte)0x0f, (byte)0x00, (byte)0x1f, (byte)0xe0,
		(byte)0x1f, (byte)0x80, (byte)0x1f, (byte)0xc0,
		(byte)0x0f, (byte)0xc0, (byte)0x3f, (byte)0x80,	
		(byte)0x07, (byte)0xe0, (byte)0x7e, (byte)0x00,
		(byte)0x03, (byte)0xf0, (byte)0xff, (byte)0x80,
		(byte)0x03, (byte)0xf5, (byte)0xff, (byte)0xe0,
		(byte)0x07, (byte)0xfd, (byte)0xff, (byte)0xf8,
		(byte)0x1f, (byte)0xfc, (byte)0xff, (byte)0xe8,
		(byte)0xff, (byte)0xe3, (byte)0xbf, (byte)0x70, 
		(byte)0xde, (byte)0x80, (byte)0xb7, (byte)0x00,
		(byte)0x71, (byte)0x10, (byte)0x4a, (byte)0x80,
		(byte)0x03, (byte)0x10, (byte)0x4e, (byte)0x40,
		(byte)0x02, (byte)0x88, (byte)0x8c, (byte)0x20,
		(byte)0x05, (byte)0x05, (byte)0x04, (byte)0x40,
		(byte)0x02, (byte)0x82, (byte)0x14, (byte)0x40,
		(byte)0x02, (byte)0x40, (byte)0x10, (byte)0x80, 
		(byte)0x02, (byte)0x64, (byte)0x1a, (byte)0x80,
		(byte)0x00, (byte)0x92, (byte)0x29, (byte)0x00,
		(byte)0x00, (byte)0xb0, (byte)0x48, (byte)0x00,
		(byte)0x00, (byte)0xc8, (byte)0x90, (byte)0x00,
		(byte)0x00, (byte)0x85, (byte)0x10, (byte)0x00,
		(byte)0x00, (byte)0x03, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x10, (byte)0x00
	};		
		
	
	//////////////////////////////////////////////////////////////////
	// This function does any needed initialization on the rendering
	// context. 
	protected static void setup(GL2 gl2, int width, int height)
	{
		// Black background
		gl2.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	}
	
	/////////////////////////////////////////////////////////////////
	// Called to draw scene
	protected static void render(GL2 gl2, int w, int h)
	{
		// Prevent a divide by zero, when window is too short
	    // (you cant make a window of zero width).
		if(h == 0)
			h = 1;
		
		gl2.glViewport(0, 0, w, h);
		
		// Reset the coordinate system before modifying
		gl2.glMatrixMode(GL2.GL_PROJECTION);
		gl2.glLoadIdentity();
		
		// Pseudo window coordinates
		glu.gluOrtho2D(0.0, (float)w, 0.0f, (float)h);
		
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
		
		int x, y;
		
		// Clear the window with current clearing color
		gl2.glClear(GL2.GL_COLOR_BUFFER_BIT);
		
		// Set color to white
		gl2.glColor3f(1.0f, 1.0f, 1.0f);
		
		// Loop through 16 rows and columns
		for(y = 0; y < 16; y++)
		{
			// Set raster position for this "square"
			gl2.glRasterPos2i(0, y * 32);
			for(x = 0; x < 16; x++)
			{
				// Draw the "fire" bitmap, advance raster position
				gl2.glBitmap(32, 32, 0.0f, 0.0f, 32.0f, 0.0f, fire, 0);
			}
		}
		
		// Do the buffer swap
		gl2.glFlush();
	}
}
